๋ฌธ์
ํฌ๊ธฐ๊ฐ NรM์ธ ์ง๋๊ฐ ์กด์ฌํ๋ค. ์ง๋์ ์ค๋ฅธ์ชฝ์ ๋์ชฝ, ์์ชฝ์ ๋ถ์ชฝ์ด๋ค. ์ด ์ง๋์ ์์ ์ฃผ์ฌ์๊ฐ ํ๋ ๋์ฌ์ ธ ์์ผ๋ฉฐ, ์ฃผ์ฌ์์ ์ ๊ฐ๋๋ ์๋์ ๊ฐ๋ค. ์ง๋์ ์ขํ๋ (r, c)๋ก ๋ํ๋ด๋ฉฐ, r๋ ๋ถ์ชฝ์ผ๋ก๋ถํฐ ๋จ์ด์ง ์นธ์ ๊ฐ์, c๋ ์์ชฝ์ผ๋ก๋ถํฐ ๋จ์ด์ง ์นธ์ ๊ฐ์์ด๋ค.
2 4 1 3 5 6
์ฃผ์ฌ์๋ ์ง๋ ์์ ์ ๋ฉด์ด 1์ด๊ณ , ๋์ชฝ์ ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ด 3์ธ ์ํ๋ก ๋์ฌ์ ธ ์์ผ๋ฉฐ, ๋์ฌ์ ธ ์๋ ๊ณณ์ ์ขํ๋ (x, y) ์ด๋ค. ๊ฐ์ฅ ์ฒ์์ ์ฃผ์ฌ์์๋ ๋ชจ๋ ๋ฉด์ 0์ด ์ ํ์ ธ ์๋ค.
์ง๋์ ๊ฐ ์นธ์๋ ์ ์๊ฐ ํ๋์ฉ ์ฐ์ฌ์ ธ ์๋ค. ์ฃผ์ฌ์๋ฅผ ๊ตด๋ ธ์ ๋, ์ด๋ํ ์นธ์ ์ฐ์ฌ ์๋ ์๊ฐ 0์ด๋ฉด, ์ฃผ์ฌ์์ ๋ฐ๋ฅ๋ฉด์ ์ฐ์ฌ ์๋ ์๊ฐ ์นธ์ ๋ณต์ฌ๋๋ค. 0์ด ์๋ ๊ฒฝ์ฐ์๋ ์นธ์ ์ฐ์ฌ ์๋ ์๊ฐ ์ฃผ์ฌ์์ ๋ฐ๋ฅ๋ฉด์ผ๋ก ๋ณต์ฌ๋๋ฉฐ, ์นธ์ ์ฐ์ฌ ์๋ ์๋ 0์ด ๋๋ค.
์ฃผ์ฌ์๋ฅผ ๋์ ๊ณณ์ ์ขํ์ ์ด๋์ํค๋ ๋ช ๋ น์ด ์ฃผ์ด์ก์ ๋, ์ฃผ์ฌ์๊ฐ ์ด๋ํ์ ๋ ๋ง๋ค ์๋จ์ ์ฐ์ฌ ์๋ ๊ฐ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ฃผ์ฌ์๋ ์ง๋์ ๋ฐ๊นฅ์ผ๋ก ์ด๋์ํฌ ์ ์๋ค. ๋ง์ฝ ๋ฐ๊นฅ์ผ๋ก ์ด๋์ํค๋ ค๊ณ ํ๋ ๊ฒฝ์ฐ์๋ ํด๋น ๋ช ๋ น์ ๋ฌด์ํด์ผ ํ๋ฉฐ, ์ถ๋ ฅ๋ ํ๋ฉด ์ ๋๋ค.
์ , ์ถ๋ ฅ
์ ๋ ฅ
- ์ฒซ์งธ ์ค์ ์ง๋์ ์ธ๋ก ํฌ๊ธฐ N, ๊ฐ๋ก ํฌ๊ธฐ M (1 โค N, M โค 20), ์ฃผ์ฌ์๋ฅผ ๋์ ๊ณณ์ ์ขํ x, y(0 โค x โค N-1, 0 โค y โค M-1), ๊ทธ๋ฆฌ๊ณ ๋ช ๋ น์ ๊ฐ์ K (1 โค K โค 1,000)๊ฐ ์ฃผ์ด์ง๋ค.
- ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์ ์ง๋์ ์ฐ์ฌ ์๋ ์๊ฐ ๋ถ์ชฝ๋ถํฐ ๋จ์ชฝ์ผ๋ก, ๊ฐ ์ค์ ์์ชฝ๋ถํฐ ๋์ชฝ ์์๋๋ก ์ฃผ์ด์ง๋ค. ์ฃผ์ฌ์๋ฅผ ๋์ ์นธ์ ์ฐ์ฌ ์๋ ์๋ ํญ์ 0์ด๋ค. ์ง๋์ ๊ฐ ์นธ์ ์ฐ์ฌ ์๋ ์๋ 10 ๋ฏธ๋ง์ ์์ฐ์ ๋๋ 0์ด๋ค.
- ๋ง์ง๋ง ์ค์๋ ์ด๋ํ๋ ๋ช ๋ น์ด ์์๋๋ก ์ฃผ์ด์ง๋ค. ๋์ชฝ์ 1, ์์ชฝ์ 2, ๋ถ์ชฝ์ 3, ๋จ์ชฝ์ 4๋ก ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
- ์ด๋ํ ๋๋ง๋ค ์ฃผ์ฌ์์ ์ ๋ฉด์ ์ฐ์ฌ ์๋ ์๋ฅผ ์ถ๋ ฅํ๋ค. ๋ง์ฝ ๋ฐ๊นฅ์ผ๋ก ์ด๋์ํค๋ ค๊ณ ํ๋ ๊ฒฝ์ฐ์๋ ํด๋น ๋ช ๋ น์ ๋ฌด์ํด์ผ ํ๋ฉฐ, ์ถ๋ ฅ๋ ํ๋ฉด ์ ๋๋ค.
๐ Logic
class Dice {
int[][] map;
int[] commands;
int[] numbers = new int[6];
int[] rangeX = { 0, 0, -1, 1 };
int[] rangeY = { 1, -1, 0, 0 };
int N, M, K;
int top = 0, front = 4, east = 2;
...
}
- ์ฃผ์ฌ์์ ์ง๋์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ํด๋์ค
- top, front, east ์ ๋ณด๋ฅผ ๊ณ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ์์ผ๋ก ์ฃผ์ฌ์์ ์ซ์ ์ ๋ณด๋ฅผ ์ถ์ ํ๋ค.
public void game(int x, int y) {
for (int i = 0; i < K; i++) {
int nx = x + rangeX[commands[i] - 1];
int ny = y + rangeY[commands[i] - 1];
if (isInTheMap(nx, ny)) continue;
x = nx;
y = ny;
rollDice(commands[i] - 1);
if (map[x][y] == 0) {
map[x][y] = numbers[5 - top];
} else {
numbers[5 - top] = map[x][y];
map[x][y] = 0;
}
System.out.println(numbers[top]);
}
}
-
๋ช ๋ น์ด์ ๋ฐ๋ผ ์ง๋์์์ ์ฃผ์ฌ์ ์์น๋ฅผ ๋ณ๊ฒฝ์ํจ๋ค.
- ์ง๋ ๋ฒ์ ์์ธ ๊ฒฝ์ฐ๋ง ์ฃผ์ฌ์ ์ ๋ณด๋ฅผ ์์ ํ๊ณ , ์๋จ์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ค.
private void rollDice(int command) {
int t, e, f;
t = top;
e = east;
f = front;
switch (command) {
case 0:
top = 5 - e;
east = t;
break;
case 1:
top = e;
east = 5 - t;
break;
case 2:
top = f;
front = 5 - t;
break;
case 3:
top = 5 - f;
front = t;
}
}
- ์ฃผ์ฌ์์ top, front, east ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๋ ๋ฉ์๋
๐ CODE
์ฝ๋ ๋ณด๊ธฐ/์ ๊ธฐ๐ซ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
class Dice {
int[][] map;
int[] commands;
int[] numbers = new int[6];
int[] rangeX = { 0, 0, -1, 1 };
int[] rangeY = { 1, -1, 0, 0 };
int N, M, K;
int top = 0, front = 4, east = 2;
public Dice(int N, int M, int K) {
this.N = N;
this.M = M;
this.K = K;
map = new int[N][M];
commands = new int[K];
}
public void game(int x, int y) {
for (int i = 0; i < K; i++) {
int nx = x + rangeX[commands[i] - 1];
int ny = y + rangeY[commands[i] - 1];
if (isInTheMap(nx, ny)) continue;
x = nx;
y = ny;
rollDice(commands[i] - 1);
if (map[x][y] == 0) {
map[x][y] = numbers[5 - top];
} else {
numbers[5 - top] = map[x][y];
map[x][y] = 0;
}
System.out.println(numbers[top]);
}
}
private boolean isInTheMap(int x, int y) {
return (x < 0 | x >= N | y < 0 | y >= M);
}
private void rollDice(int command) {
int t, e, f;
t = top;
e = east;
f = front;
switch (command) {
case 0:
top = 5 - e;
east = t;
break;
case 1:
top = e;
east = 5 - t;
break;
case 2:
top = f;
front = 5 - t;
break;
case 3:
top = 5 - f;
front = t;
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] line = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int N = line[0], M = line[1], x = line[2], y = line[3], K = line[4];
Dice dice = new Dice(N, M, K);
for (int i = 0; i < N; i++) dice.map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
dice.commands = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
dice.game(x, y);
br.close();
}
}
โ๏ธ Review
-
๋ค๋ฅธ ๊ตฌํ์ ๋ชจ๋ ์ฌ์ ๋๋ฐ,
rollDice()๊ตฌํ์ด ์ ์ผ ์ด๋ ค์ ๋ค.- ์๋๋ ๋ช ๋ น์ด๊ฐ ์ฃผ์ด์ง๋ฉด, ์ ์ญ์ผ๋ก ๊ฐ์ง๊ณ ์๋ bottom์ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ ค ํ๋๋ฐ, ์๋ฌด๋ฆฌ ์๊ฐํด๋ ์๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ ๊ฒ ๊ฐ์ ๊ฒ์ํ์ ์ฐธ๊ณ ํ๋ค.
๐ ์ถ์ฒ
Baekjoon : https://www.acmicpc.net/problem/14499